package cn.kent.fixsidetouch;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

public class SideBar {
    public static final int SideBarColor = 0x33000000;
    public static final int Transparent = Color.TRANSPARENT;

    public static final class HOLDER {
        @SuppressLint("StaticFieldLeak")
        private static final SideBar INSTANCE = new SideBar();
    }

    private SideBar() {
        mWm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    }

    public static SideBar getInstance() {
        return HOLDER.INSTANCE;
    }

    private View sideBarLeft;
    private View sideBarRight;
    private boolean showColor;
    private final WindowManager mWm;

    public void updateSideBar() {
        boolean enabled = PrefUtils.getInstance().getSideBarEnabled();
        if (enabled) {
            showSideBar();
        } else {
            hideSideBar();
        }
    }

    private Context getContext() {
        return Utils.getContext();
    }

    private void showSideBar() {
        Context context = getContext();
        int percent = PrefUtils.getInstance().getSideBarPercent();
        if (sideBarLeft == null) {
            sideBarLeft = new View(context);
            sideBarLeft.setBackgroundColor(getSideBarColor());
        }
        if (sideBarRight == null) {
            sideBarRight = new View(context);
            sideBarRight.setBackgroundColor(getSideBarColor());
        }
        DisplayMetrics dm = new DisplayMetrics();
        mWm.getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;
        int viewWidth = (int) (screenWidth * percent / 100.0);

        if (sideBarLeft.getParent() == null) {
            mWm.addView(sideBarLeft, generateLp(viewWidth, true));
        } else {
            ViewGroup.LayoutParams lp = sideBarLeft.getLayoutParams();
            lp.width = viewWidth;
            sideBarLeft.setLayoutParams(lp);
        }
        if (sideBarRight.getParent() == null) {
            mWm.addView(sideBarRight, generateLp(viewWidth, false));
        } else {
            ViewGroup.LayoutParams lp = sideBarRight.getLayoutParams();
            lp.width = viewWidth;
            sideBarRight.setLayoutParams(lp);
        }
        Toast.makeText(getContext(), "width:" + viewWidth, Toast.LENGTH_SHORT).show();
    }

    public void showColor(boolean showColor) {
        if (sideBarLeft == null || sideBarRight == null) {
            return;
        }
        this.showColor = showColor;
        int color = getSideBarColor();
        sideBarLeft.setBackgroundColor(color);
        sideBarRight.setBackgroundColor(color);
    }

    private int getSideBarColor() {
        return showColor ? SideBarColor : Transparent;
    }

    private WindowManager.LayoutParams generateLp(int width, boolean sideLeft) {
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.width = width;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        lp.gravity = sideLeft ? Gravity.START : Gravity.END;
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH;
        lp.format = PixelFormat.RGBA_8888;
        lp.type = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        return lp;
    }

    public void hideSideBar() {
        if (sideBarLeft != null) {
            mWm.removeView(sideBarLeft);
            sideBarLeft = null;
        }
        if (sideBarRight != null) {
            mWm.removeView(sideBarRight);
            sideBarRight = null;
        }
    }
}
